<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.util.Date" %>
<%@ page import="ut.data.User" %>
<%@ page import="ut.data.Record" %>
<%@ page import="ut.data.Profile" %>
<%@ page import="ut.data.Task" %>
<%@ page import="ut.res.Language" %>
<%@ page import="ut.util.DateTime" %>
<%
	User user = (User)session.getAttribute("user");
	if(user == null){
		response.sendRedirect("/");
		return;
	}
	
	int p = 1;
	long tid = 0;
	if(request.getParameter("tid") == null){
		response.sendRedirect("/account/user-info.jsp");
		return;
	}
	try{
		tid = Long.parseLong(request.getParameter("tid"));
	}catch(NumberFormatException e){
		response.sendRedirect("/account/user-info.jsp");
		return;
	}
	try{
		if(request.getParameter("p") != null){
			p = Integer.parseInt(request.getParameter("p"));
		}
	}catch(NumberFormatException e){
		p = 1;
	}
	
	Task task = Task.getTask(tid);
	if(task == null || (task.employer != user.id && task.translator != user.id)){
		response.sendRedirect("/account/user-info.jsp");
		return;
	}
	int maxPage = task.getMaxApplicationPage(tid, 10);
		if(p > maxPage){
			p = maxPage;
	}
	
	String lang =
		Language.getLanguage(task.fromLan).title + " <img src='/image/arrow.png' /> "	+ Language.getLanguage(task.toLan).title;
%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>UT終極一翻！ | 付費案件#<%= tid %></title>
	<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
	<link rel="stylesheet" type="text/css" href="/css/task-detail.css" media="all" />
	<script type="text/javascript" src="/js/Utility.js"></script>
	<script type="text/javascript" src="/js/Message.js"></script>
	<script type="text/javascript" src="/js/Ajax.js"></script>
	<script type="text/javascript" src="/js/Button.js"></script>
	<script type="text/javascript" src="/js/Account.js"></script>
	<script type="text/javascript" src="/js/Task.js"></script>
	<script type="text/javascript" src="/script.js"></script>
</head>
<body onload="init();">
<%@ include file="/include/banner.jsp" %>
	<div class="content">
		<div class="title">付費發案管理#<%= tid %></div>
<%
	//案件結案後的評價
	if(task.state >= Task.ACCEPTED){
%>
		<div class="task block">
			<span class="tag top">案件評價</span>
			<div class="task-content">
				<div>
					發案者給予譯者的評價：
<%
		double score = task.translatorScore;
		for(int i = 1; i <= 5; i++){
			if(score >= 1){
%>
					<img src="/image/star-2.png" class="image-middle" />
<%
			}else{
%>
					<img src="/image/star-0.png" class="image-middle" />
<%
			}
			score--;
		}
		if(task.translatorScore == 0){
%>
					<span class="mark-red">（尚未給予評價）</span>
<%
		}
%>
				</div>
				<div>
					譯者給予發案者的評價：
<%
		score = task.employerScore;
		for(int i = 1; i <= 5; i++){
			if(score >= 1){
%>
					<img src="/image/star-2.png" class="image-middle" />
<%
			}else{
%>
					<img src="/image/star-0.png" class="image-middle" />
<%
			}
			score--;
		}
		if(task.employerScore == 0){
%>
					<span class="mark-red">（尚未給予評價）</span>
<%
		}
%>
				</div>
			</div>
			<div class="task-bottom">
<%
	if(task.employer == user.id){
		if(task.translatorScore == 0){
%>
				<span class="tag bottom tool"
					onclick="Task.showEvaluate(<%= task.id %>, 'translator', '<%= User.getUserName(task.translator) %>')">評價譯者</span>
<%
		}
		if(task.employerScore == 0 && System.currentTimeMillis() - task.finishTime.getTime() < 259200000){ // 三天內可邀請
%>
				<span class="tag bottom tool" onclick="Task.inviteEvaluation(<%= task.id %>, this);">邀請譯者評價</span>
<%
		}
	}else{
		if(task.employerScore == 0){
%>
				<span class="tag bottom tool"
					onclick="Task.showEvaluate(<%= task.id %>, 'employer', '<%= User.getUserName(task.employer) %>')">評價發案者</span>
<%
		}
		if(task.translatorScore == 0 && System.currentTimeMillis() - task.finishTime.getTime() < 259200000){ // 三天內可邀請
%>
				<span class="tag bottom tool" onclick="Task.inviteEvaluation(<%= task.id %>, this);">邀請發案者評價</span>
<%
		}
	}
%>
			</div>
		</div>
<%
	}
%>
		<div class="task block">
			<span class="tag top blue"><%= lang %></span>
			<div class="task-content">
				<div class="task-element">
					<div class="task-element-left">案主：</div>
					<div class="task-element-right">
						<a href="/account/member-info.jsp?uid=<%= task.employer %>&view=1"><%= User.getUserName(task.employer) %></a>
					</div>
				</div>
				
				<div class="task-element">
					<div class="task-element-left">案件價格：</div>
					<div class="task-element-right">NTD <%= task.price%></div>
				</div>

				<div class="task-element">
					<div class="task-element-left">字數統計：</div>
					<div class="task-element-right">字數：<%= task.wordNumber %>，字元數：<%= task.characterNumber %>（包含空白字元）</div>
				</div>

				<div class="task-element">
					<div class="task-element-left">發案日期：</div>
					<div class="task-element-right"><%= DateTime.toDateTime(task.publishTime)%></div>
				</div>
<%
	if(task.matchTime != null){
%>
				<div class="task-element">
					<div class="task-element-left">媒合日期：</div>
					<div class="task-element-right"><%= DateTime.toDateTime(task.matchTime)%></div>
				</div>
<%
	}
	if(task.translateTime != null){
%>
				<div class="task-element">
					<div class="task-element-left">交稿日期：</div>
					<div class="task-element-right"><%= DateTime.toDateTime(task.translateTime)%></div>
				</div>
<%
	}
%>			
				<div class="task-element">
					<div class="task-element-left">截止日期：</div>
					<div class="task-element-right"><%= DateTime.toDateTime(task.deadTime)%></div>
				</div>
<%
	if(task.finishTime != null){
%>
				<div class="task-element">
					<div class="task-element-left">結案日期：</div>
					<div class="task-element-right"><%= DateTime.toDateTime(task.finishTime)%></div>
				</div>
<%
	}
%>	
				<div class="task-element">
					<div class="task-element-left">案件狀態：</div>
<%
	if(task.state == Task.MATCHING){
%>
					<div class='task-element-right mark-green'>媒合中</div>
<%
	}else if(task.state == Task.MATCHED){
%>
					<div class='task-element-right mark-blue'>等待接案譯者上傳譯文</div>
<%
	}else if(task.state == Task.TRANSLATED){
%>
					<div class='task-element-right mark-blue'>等待案主審閱譯文</div>
<%
	}else if(task.state == Task.ACCEPTED){
%>
					<div class='task-element-right mark-red'>案件成交（全額支付款項）</div>
<%
	}else if(task.state == Task.REJECTED){
%>
					<div class='task-element-right mark-red'>案主退件（仍需支付訂金）</div>
<%
	}else if(task.state == Task.ABANDONED){
%>
					<div class='task-element-right mark-red'>接案譯者放棄案件（全額退款）</div>
<%
	}else if(task.state == Task.DEAD){
%>
					<div class='task-element-right mark-red'>案件逾期，媒合失敗（全額退款）</div>
<%
	}else{
%>
					<div class='task-element-right mark-red'>案件逾期，案主撤回（全額退款）</div>
<%
	}
%>
				</div>
				
				<div class="task-element">
					<div class="task-element-left">案件領域：</div>
					<div class="task-element-right"><%= task.domain.replace("&lt;br/&gt;", " ")%></div>
				</div>
				
				<div class="task-element">
					<div class="task-element-left">案件說明：</div>
					<div class="task-element-right"><%= task.description.replace("&lt;br/&gt;", " ")%></div>
				</div>
				
				<div class="task-element">
					<div class="task-element-left">翻譯原文：</div>
					<div class="task-element-right">
						<textarea readonly><%= task.content.replace("<br/>", "\r\n") %></textarea>
					</div>
				</div>
				
				<div style="clear:both"></div>
			</div>
			<div class="task-bottom">
<%
	if(user.id == task.employer){
		if(task.state == Task.MATCHING){
%>
				<span class="tag bottom tool blue" onclick="Task.popup('/task/task-invite.jsp?tid=<%= task.id %>')">
					從關注列表中邀請使用者接案
				</span>
<%
		}
		if(task.state == Task.MATCHED){	// 案主撤回案件功能
			if(task.deadTime.before(new Date())){
%>
				<span class="tag bottom tool blue" onclick="Task.revokeTask(<%= task.id %>)">撤回案件</span>
<%
			}else{
%>
				<span class="tag bottom tool blue" onclick="Task.revokeTask(-1)">撤回案件</span>
<%
			}
		}
	}
	if(task.state == Task.MATCHING){
%>
				<span class="tag bottom tool blue" onclick="Task.popup('/task/task-question.jsp', <%= task.id %>)">
					問與答 (<%= task.question %>)
				</span>
<%
	}else{
%>
				<span class="tag bottom tool blue" onclick="Task.popup('/task/task-discussion.jsp', <%= tid %>)">案件討論</span>
<%
	}
%>
			</div>
		</div>
<%
	// 接案申請列表
	if(user.id == task.employer && task.state == Task.MATCHING){
%>
		<div class="page">
<%
		StringBuilder pageOptions = new StringBuilder("");
		if(p > 1){
			pageOptions.append("<a href=\"./?page=1\">第一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p-1) + "\">上一頁</a>");
		}else{
			pageOptions.append("<span class=\"inactive\">第一頁</span><span class=\"page-link inactive\">上一頁</span>");
		}
		int startPage = (p - 2 > 0) ? p - 2 : 1;
		int endPage = startPage + 4;
		if(endPage > maxPage){
			startPage = startPage - (endPage - maxPage);
			endPage = maxPage;
		}
		for(int i = startPage; i <= endPage; i++){
			if(i < 1){
				continue;
			}else if(i == p){
				pageOptions.append("<span class=\"page-link current\">" + i + "</span>");
			}else{
				pageOptions.append("<a class=\"page-link\" href=\"./?page=" + i + "\">" + i + "</a>");
			}
		}
		if(p < maxPage){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p+1) + "\">下一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + maxPage + "\">最後頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">下一頁</span><span class=\"page-link inactive\">最後頁</span>");
		}
		out.println(pageOptions.toString());
%>
		</div>
<%
		Task.Application[] apps = task.getApplications(tid, 10, p);
		Record record = null;
		Profile profile = null;
		for(int i = 0; i < apps.length; i++){
			if(apps[i] == null){
				if(i == 0){
%>
		<div class="task block">
			<div class="block-center">目前沒有任何接案申請</div>
		</div>
<%
				}
				break;
			}
			
			record = Record.getRecord(apps[i].translator);
			profile = Profile.getProfile(apps[i].translator);
			if(record == null || profile == null){
				continue;
			}
			
			String spec = "";
			if(profile != null){
				Language fromLang = null;
				Language toLang = null;
				for(int j = 0; j < profile.specialties.length; j++){
					fromLang = Language.getLanguage(profile.specialties[j][0]);
					toLang = Language.getLanguage(profile.specialties[j][1]);
					spec += "<span class='spec-lang mark-red'>" + fromLang.title + "</span>";
					spec += " <img src='/image/arrow.png' /> ";
					spec += "<span class='spec-lang mark-red'>" + toLang.title + "</span>";
					
					if(j != profile.specialties.length - 1){
						spec += "；";
					}
				}
			}
%>

		<div class="task block">
			<span class="tag top green">接案申請 #<%= (i + 1) %></span>
			<div class="task-content">
				<div class="task-element">
					<div class="task-element-left">申請者：</div>
					<div class="task-element-right">
						<a href="/account/member-info.jsp?uid=<%= apps[i].translator %>&view=2"><%= User.getUserName(apps[i].translator) %></a>
					</div>
				</div>
			
				<div class="task-element">
					<div class="task-element-left">申請日期：</div>
					<div class="task-element-right"><%= DateTime.toDateTime(apps[i].applyTime) %></div>
				</div>
				<div class="task-element">
					<div class="task-element-left">翻譯專長：</div>
					<div class="task-element-right"><%= spec %></div>
				</div>
				<div class="task-element">
					<div class="task-element-left">接案綜合評價：</div>
					<div class="task-element-right">
<%
			if(record.scoreNumber == 0){
%>
						<img src="/image/star-0.png" class="image-middle" />
						<img src="/image/star-0.png" class="image-middle" />
						<img src="/image/star-0.png" class="image-middle" />
						<img src="/image/star-0.png" class="image-middle" />
						<img src="/image/star-0.png" class="image-middle" />
<%
			}else{
				double score = record.score / record.scoreNumber;
				for(int j = 1; j <= 5; j++){
					if(score >= 1){
%>
						<img src="/image/star-2.png" class="image-middle" />
<%
					}else if(score <= 0){
%>
						<img src="/image/star-0.png" class="image-middle" />
<%
					}else{
%>
						<img src="/image/star-1.png" class="image-middle" />
<%
					}
					score--;
				}
			}
%>
						<br/>
						<span class="mark-gray comment">
							（<%= record.scoreNumber %> 人評價；付費案件接案數：<%= record.response %>；
							付費案件成功數：<%= record.success %>；免費案件最佳翻譯數：<%= record.freeSuccess %>）
						</span>
					</div>
				</div>
			</div>
			<div class="task-bottom">
				<span class="tag bottom green tool" onclick="Task.assignTask(<%= tid %>, <%= apps[i].translator %>)">
					選為接案譯者
				</span>
			</div>
		</div>
<%
		}
%>		
		<div class="page">
<%
		out.println(pageOptions.toString());
%>		
		</div>
<%
	}else if(task.state != Task.DEAD){
%>
		<div class="task block">
			<span class="tag top green">
				接案譯者：<a href='/account/member-info.jsp?uid=<%= task.translator %>&view=2'><%= User.getUserName(task.translator) %></a>
			</span>
<%
		String translation = task.translation == null ? "" : task.translation.replace("<br/>", "\r\n");
		if(task.state == Task.MATCHED){	//等待接案者完成稿件
			if(user.id == task.employer){	//發案者角度
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>
						翻譯譯文：
					</div>
					<div class='task-element-right'>
						尚未上傳稿件
					</div>
				</div>
				<div style='clear:both'></div>
			</div>
<%
			}else{	//接案者角度
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>翻譯譯文：</div>
					<div class='task-element-right'>
<%
		if(user.id == task.translator){
%>
						<div class="mark-red comment">＊限 500000 字，請在案件截止日期前交件。</div>
<%
		}
%>
						<textarea id="translation"><%= translation %></textarea>
					</div>
				</div>
				<div style="clear:both"></div>
			</div>
			<div class="task-bottom">
				<span class="tag bottom tool green" onclick="Task.updateTranslation(<%= task.id %>, 'translation')">儲存草稿</span>
				<span class="tag bottom tool green" onclick="Task.confirmTranslation(<%= task.id %>, 'translation')">確認交件</span>
				<span class="tag bottom tool green" onclick="Task.abandonTask(<%= task.id %>)">放棄翻譯此案件</span>
			</div>
<%
			}
		}else if(user.id == task.employer && task.state == Task.TRANSLATED){	//發案者尚未審核譯文
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>翻譯譯文：</div>
					<div class='task-element-right'>
						<div class="mark-red comment">＊目前僅顯示 50% 譯文。欲取得全部譯文請選擇成交並支付所有款項。</div>
						<textarea readonly><%= translation.substring(0, translation.length() / 2) %> ......</textarea>
					</div>
				</div>
				<div style="clear:both"></div>
			</div>
			<div class="task-bottom">
				<span class="tag bottom tool green" onclick="Task.acceptTranslation(<%= task.id %>)">成交</span>
				<span class="tag bottom tool green" onclick="Task.rejectTranslation(<%= task.id %>)">退件</span>
			</div>
<%
		}else if(user.id == task.employer && task.state == Task.ABANDONED){	//接案者放棄
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>翻譯譯文：</div>
					<div class='task-element-right'>
						接案者已放棄翻譯
					</div>
				</div>
				<div style="clear:both"></div>
			</div>
<%
		}else if(user.id == task.employer && task.state == Task.REVOKED){	//發案者撤回逾期案件
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>翻譯譯文：</div>
					<div class='task-element-right'>
						案件已逾期撤回
					</div>
				</div>
				<div style="clear:both"></div>
			</div>
<%
		}else if(user.id == task.employer && task.state == Task.REJECTED){	//發案者退件
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>翻譯譯文：</div>
					<div class='task-element-right'>
						譯文已退件
					</div>
				</div>
				<div style="clear:both"></div>
			</div>
<%
		}else{	//案件成交
%>
			<div class='task-content'>
				<div class='task-element'>
					<div class='task-element-left'>翻譯譯文：</div>
					<div>
						<textarea readonly><%= translation %></textarea>
					</div>
				</div>
				<div style="clear:both"></div>
			</div>
<%
			if(task.state == task.ACCEPTED && task.employer == user.id && System.currentTimeMillis() - task.finishTime.getTime() < 259200000){ // 三天內
%>
			<div class="task-bottom">
				<span class="tag bottom green tool" 
					onclick="Task.createAbuseReport(<%= ut.data.AbuseReport.TASK %>, <%= task.id %>, this)">提出申訴</span>
			</div>
<%
			}
		}
%>
		</div>
<%
	}
%>
<%@ include file="/include/foot.jsp" %>
	</div>
</body>
</html>